﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections;
using System.Web.Script.Serialization;
using System.Data;

namespace HQWeb.View
{
    /// <summary>
    /// GetShop 获得购物车信息
    /// </summary>
    public class GetShop : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            string strids = context.Request["ids"];
            string strType = context.Request["type"];
            //  string[] strArry = strids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            JavaScriptSerializer js = new JavaScriptSerializer();
            if (strType.Equals("all"))
            {
                context.Response.Write(js.Serialize(GetAllCOntent(strids)));
            }
            else
            {
                context.Response.Write(js.Serialize(GetOneMolde(strids)));
            }
        }

        private ProgectFile GetOneMolde(string strID)
        {
            BLL.Progect.T_PUB_PROJECT progectBll = new BLL.Progect.T_PUB_PROJECT();
            DataTable table = progectBll.GetProgectImage(strID);
            ProgectFile p = new ProgectFile();
            string strType;
            string strUrl;
            if (table.Rows.Count > 0)
            {
                DataRow rowTemp = table.Rows[0];
                strType = rowTemp["TYPE"].ToString();
                if (strType == "1")
                {
                    strUrl = rowTemp["URLThumbnail"].ToString();
                }
                else
                {
                    strUrl = rowTemp["URLThumbnail"].ToString();
                }
                p.ids = rowTemp["id"].ToString();
                p.image = "../" + strUrl.Replace("//", "/");
                p.name = rowTemp["name"].ToString();
                p.price = rowTemp["price"].ToString();
                p.strUrl = rowTemp["URLOriginal"].ToString();
                p.Type = rowTemp["type"].ToString();
            }
            return p;
        }

        /// <summary>
        /// 获得所有购物车数据
        /// </summary>
        /// <param name="strIDs"></param>
        /// <returns></returns>
        private ArrayList GetAllCOntent(string strIDs)
        {
            BLL.Progect.T_PUB_PROJECT progectBll = new BLL.Progect.T_PUB_PROJECT();
            DataTable table = progectBll.GetProgectImage(strIDs);
            int count = table.Rows.Count;
            DataRow rowTemp;
            string strExistIDs = "";
            string strID = "";
            ArrayList modelList = new ArrayList();
            string strType;
            string strUrl;
            for (int i = 0; i < count; i++)
            {
                rowTemp = table.Rows[i];
                strID = rowTemp["id"].ToString();
                if (strExistIDs.IndexOf(strID + ",") == -1)
                {
                    strExistIDs = strExistIDs + strID + ",";
                    strType = rowTemp["TYPE"].ToString();
                    if (strType == "1")
                    {
                        strUrl = rowTemp["URLThumbnail"].ToString();
                    }
                    else
                    {
                        strUrl = rowTemp["URLThumbnail"].ToString();
                    }
                    ProgectFile p = new ProgectFile();
                    p.ids = strID;
                    p.image = "../" + strUrl.ToString().Replace("//", "/");
                    p.name = rowTemp["name"].ToString();
                    p.price = rowTemp["price"].ToString();
                    p.strUrl = rowTemp["URLOriginal"].ToString();
                    p.Type = rowTemp["type"].ToString();
                    p.strProjectType = rowTemp["PROJECTTYPE_NAME"].ToString();
                    modelList.Add(p);
                }
            }
            return modelList;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class ProgectFile
        {
            public string ids = "";       //项目ID
            //   [DataMember]
            public string price = "";      //项目价格
            //   [DataMember]
            public string image = "";      //项目缩率图地址
            //   [DataMember]
            public string name = "";       //项目名称
            public string fileID = "";     //文件ID
            public string strUrl = "";     //文件原始图片地址
            public string Type = "";       //文件类型
            public string strProjectType = "";
        }
    }
}